package com.it135.citytraffic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.it135.citytraffic.pojo.dto.UserRoadReasonDTO;
import com.it135.citytraffic.pojo.entity.UserRoadReason;
import com.it135.citytraffic.mapper.UserRoadReasonMapper;
import com.it135.citytraffic.pojo.vo.UserRoadReasonVO;
import com.it135.citytraffic.service.IUserRoadReasonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.it135.citytraffic.utils.ReasonsUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 用户提交的路段拥堵原因 服务实现类
 * </p>
 *
 * @author lh
 * @since 2024-07-17
 */
@EqualsAndHashCode(callSuper = true)
@Service
@Data
public class UserRoadReasonServiceImpl extends ServiceImpl<UserRoadReasonMapper, UserRoadReason> implements IUserRoadReasonService {

    private final UserRoadReasonMapper userRoadReasonMapper;

    @Override
    public void submitReason(UserRoadReasonDTO userRoadReasonDTO) {
        String region = userRoadReasonDTO.getRegion();
        String roadName = userRoadReasonDTO.getRoadName();

//        1.查询之前是否有这种原因
        LambdaQueryWrapper<UserRoadReason> like = new LambdaQueryWrapper<UserRoadReason>()
                .like(UserRoadReason::getRegion, region)
                .like(UserRoadReason::getRoadName, roadName)
                .eq(UserRoadReason::getReason, userRoadReasonDTO.getReason());
        List<UserRoadReason> userRoadReasons = userRoadReasonMapper.selectList(like);

//        2.没有直接插入,count为1
        if( userRoadReasons.size() == 0) {
            UserRoadReason roadReason = new UserRoadReason();
            BeanUtils.copyProperties(userRoadReasonDTO, roadReason);
            roadReason.setCount(1);
            roadReason.setReason(userRoadReasonDTO.getReason());
            userRoadReasonMapper.insert(roadReason);
        }
//        2.有,count+1
        else {
            UserRoadReason roadReason = userRoadReasons.get(0);
            userRoadReasonMapper.update(new LambdaUpdateWrapper<UserRoadReason>()
                    .eq(UserRoadReason::getId,roadReason.getId())
                    .setSql("count=count+1"));
        }
    }

    @Override
    public  List<UserRoadReason> findAll() {
        return userRoadReasonMapper.findAll();
    }

    /**
     * 意见反馈次数
     * @return
     */
    @Override
    public UserRoadReasonVO getCount() {
        HashMap<Integer, Integer> hm = new HashMap<>();
        List<UserRoadReason> userRoadReasonList = userRoadReasonMapper.selectList(new LambdaQueryWrapper<UserRoadReason>()
                .select(UserRoadReason::getReason, UserRoadReason::getCount));
        for (UserRoadReason userRoadReason : userRoadReasonList) {
            Integer reason = userRoadReason.getReason();
            Integer count = userRoadReason.getCount();
            if (hm.containsKey(reason)) {
                hm.put(reason, hm.get(reason) + count);
            } else {
                hm.put(reason, count);
            }
        }
        List<String> reason = new ArrayList<>();
        List<Integer> reasonCount = new ArrayList<>();
        List<Integer> value = new ArrayList<>(hm.values());
        Collections.sort(value);
        value.sort(Collections.reverseOrder());
        for (int i = 0; i <= 5; i++) {
            reasonCount.add(value.get(i));
        }

        for (Integer position : reasonCount) {
            for (Integer key : hm.keySet()) {
                if (hm.get(key).equals(position)) {
                    String reasonStr = ReasonsUtils.getReason(key);
                    reason.add(reasonStr);
                    hm.remove(key);
                    break;
                }
            }
        }

        Long allCount = userRoadReasonMapper.getCount();

        return UserRoadReasonVO
                .builder()
                .reason(reason)
                .reasonCount(reasonCount)
                .allCount(allCount)
                .build();
    }
}
